﻿using LitJson;
using RestAPI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TopVisionVideoStream.TopVisionCenter;

namespace TopVisionVideoStream.Mananger
{
    public static class TVCVideosManager
    {
        private static List<ConfVideo> confVideos;

        public static List<ConfVideo> ConfVideos
        {
            get
            {
                if (confVideos == null) confVideos = new List<ConfVideo>();

                return confVideos;
            }

            set
            {
                confVideos = value;
            }
        }

        //private static string httpHeaderPath => string.Format("{0}:{1}/{2}/", TVVS.ServerUrl, "", "nfs");

        private static bool isInited = false;

        public static bool IsDispose = false;

        /// <summary>
        /// 更新视频列表的间隔(默认3秒)
        /// </summary>
        public static TimeSpan Interval = TimeSpan.FromSeconds(3);

        public static void Init(bool fromTVCenter)
        {
            if (isInited) return;

            IsDispose = false;

            isInited = true;

            if (fromTVCenter)
            {
                TVCUpdate();
            }
            else
            {
                Update();
            }
        }

        private static async void Update()
        {
            while (!IsDispose)
            {
                await UpdateVideoAsync();

                await Task.Delay(Interval);
            }
        }

        private static async Task UpdateVideoAsync()
        {
            Mzwangetvideopath mApi = new Mzwangetvideopath(Configuration.CompanyID);

            ConfVideoResult videoResult = new ConfVideoResult();

            bool getResult = false;

            TimeSpan delay = TimeSpan.FromSeconds(1);

            mApi.Call((result) =>
            {
                getResult = true;

                videoResult = JsonMapper.ToObject<ConfVideoResult>(result);
            });

            while (!getResult)
            {
                await Task.Delay(delay);
            }

            var videos = videoResult.data;

            var videosDel = ConfVideos.Except(videos);

            foreach (var item in videosDel)
            {
                ConfVideos.Remove(item);
            }

            var videosAdd = videos.Except(ConfVideos);

            foreach (var item in videosAdd)
            {
                if (item.confrecord.ToLower().EndsWith("null") | item.confrecord.ToLower().EndsWith("na")) continue;

                ConfVideos.Add(item);
            }
        }

        private static async void TVCUpdate()
        {
            while (!IsDispose)
            {
                await TVCUpdateVideoAsync();

                await Task.Delay(Interval);
            }
        }

        private static async Task TVCUpdateVideoAsync()
        {
            TVCConferenceVideosModel model = new TVCConferenceVideosModel();

            model.companyid = Configuration.CompanyID;

            TVCenter center = new TVCenter();

            TVCenterResponse result = null;

            await Task.Factory.StartNew(async () =>
             {
                 result = await center.ConferenceVideos(model);
             });

            while (result == null)
            {
                await Task.Delay(100);
            }

            if (result.success)
            {
                var confsInfo = result.data.confInfo;

                string json = JsonMapper.ToJson(confsInfo);

                ConfVideos = JsonMapper.ToObject<List<ConfVideo>>(json);
            }
        }

        public static void Dispose()
        {
            IsDispose = true;

            isInited = false;

            confVideos = null;
        }
    }
}
